package com.mccree.codec2;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

public class TestProtoServerHandler2 extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> {
    @Override
    protected void channelRead0(ChannelHandlerContext ctx, MyDataInfo.MyMessage msg) throws Exception {

        System.out.println("msg="+msg.toString());
//        if (MyDataInfo.MyMessage.DataType.StudentType == msg.getDataType()) {
//            MyDataInfo.Student student = msg.getStudent();
//            System.out.println("studentName="+ student.getName()+"\tStudentId="+student.getId());
//        }else if (msg.getDataType() == MyDataInfo.MyMessage.DataType.WorkerType){
//            MyDataInfo.Worker worker = msg.getWorker();
//            System.out.println("workerName="+worker.getName()+"\tworkerAge"+worker.getAge());
//        }else {
//            System.out.println("传输的数据格式有误");
//        }
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.writeAndFlush(Unpooled.copiedBuffer("我是服务器，客户端你好呀！", CharsetUtil.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
        cause.printStackTrace();
    }
}
